package edu.hawaii.duedates.book;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;


/**
 * The Book Class implements the IBook interface.  A book is comprised of a title and a due date.
 * 
 * @author Ronn Reeves and Robin E. Raqueno
 */
public class Book implements IBook {
  
  /**  The book title.  */
  private String title;

  /** The due date for the book. */
  private String dueDate;
  
  /** The library the book belongs to. */
  private String library;
  
  
  /**
   * Creates a book object with the given title and due date.
   * 
   * @param title  The book title.
   * @param dueDate  The due date of the book.
   * @param library The library the book belongs to.
   */
  public Book(String title, String dueDate, String library) {
    this.formatTitle(title);
    this.formatDueDate(dueDate);
    this.library = library;
  }

  
  /**
   * Sets the title of this book.
   * 
   * @param title  The book title.
   */
  public void setTitle(String title) {
    this.formatTitle(title);
  }
  /**
   * Returns the title of this book.  
   *
   * @return String  The book title.
   */
  public String getTitle() {
    return this.title;
  }
  
  
  /**
   * Strips up to 75 characters of the title of this book from extra data in the title string.
   * 
   * @param title  The book title (up to 75 characters).
   */
  private void formatTitle(String title) {
    String s1 = title;
    
    if ((s1.contains("/")) && (s1.contains("Location:")) && (s1.contains("  "))) {
      // The title lies between the first occurrence of "  " and the last occurrence of "/"
      // in the displayed out of the University of Hawaii website.
      s1 = s1.substring(s1.indexOf("  ") + 2, s1.lastIndexOf('/') - 1);
    }
    
    if (s1.length() > 75) {
      this.title = s1.substring(0, 75);
    }
    else {
      this.title = s1;
    }
  }
  

  /**
   * Sets the due date of this book.  
   * 
   * @param  dueDate  The due date of the book.
   */
  public void setDueDate(String dueDate) {
    this.formatDueDate(dueDate);
  }
  /**
   * Returns the due date for this book.
   *
   * @return Date  The due date for the book.
   */
  public String getDueDate() {
    return this.dueDate;
  }

  
  /**
   * Standardizes the due date of this book.
   * 
   * @param  dueDate  The due date of the book.
   */
  private void formatDueDate(String dueDate) {
    if (dueDate.length() > 10) {
      String s1 = dueDate.substring(dueDate.length() - 8);
      this.dueDate = s1.replace('/', '-');
    }
    else {
      String s1  = dueDate.replace('/', '-');
      this.dueDate = s1.substring(0, 6) + s1.substring(8, 10);
    }
  }

  
  /**
   * Sets the library the book is from.
   * 
   * @param library  The library the book is from.
   */
  public void setLibrary(String library) {
    this.library = library;
  }
  /**
   * Returns the library the book is from.
   * 
   * @return Library  The library the book is from.
   */
  public String getLibrary() {
    return this.library;
  }

  
  /**
   * Compares this book's due date to the caller's book's due date.
   * 
   * @param book The book to be compared.
   * @return The sort order compared to this book.
   */
  public int compareByDate(Book book) {
    String s1 = book.getDueDate();
    String s2 = this.dueDate;
    SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy", Locale.US);
    try {
      Date thisDate = dateFormat.parse(s2);
      Date compareDate = dateFormat.parse(s1);
      return thisDate.compareTo(compareDate);
    }
    
    catch (ParseException e) {
      e.printStackTrace();
    }
    
    return 0;
  }

  
  /**
   * Compares this book's library to the calling book's library.
   * 
   * @param book The book to be compared.
   * @return The sort order compared to this book.
   */
  public int compareByLibrary(Book book) {
    return library.compareToIgnoreCase(book.getLibrary());
  }

  
  /**
   * Returns the title of this book.  
   *
   * @return String  The book title.
   */
  public String toString() {
    return this.getTitle();
  }
}